package com.lhnstart.learn.bases.arrays_;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 小练习
 *
 * @author lhn
 * @date 2023/08/07 10:53
 * @since 1.0.0
 **/
public class ArrayExercise {

    public static void main(String[] args) {
        Book_[] book_s = new Book_[4];
        book_s[0] = new Book_("红楼梦~", 100.00);
        book_s[1] = new Book_("金瓶梅~", 90.00);
        book_s[2] = new Book_("青年文摘~", 5.00);
        book_s[3] = new Book_("java从入门到放弃~", 300.00);
        // 按照价格升序
        //Arrays.sort(book_s, new Comparator <Book_>() {
        //    @Override
        //    public int compare(Book_ o1, Book_ o2) {
        //        if (o1.getPrice() - o2.getPrice() > 0) {
        //            return 1;
        //        } else {
        //            return -1;
        //        }
        //    }
        //});
        // 按照价格倒序
        //Arrays.sort(book_s, new Comparator <Book_>() {
        //    @Override
        //    public int compare(Book_ o1, Book_ o2) {
        //        if (o1.getPrice() - o2.getPrice() > 0) {
        //            return -1;
        //        } else {
        //            return 1;
        //        }
        //    }
        //});
        // 按照书名的长度升序
        Arrays.sort(book_s, new Comparator <Book_>() {
            @Override
            public int compare(Book_ o1, Book_ o2) {
                if (o1.getName().length() - o2.getName().length() > 0) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });
        System.out.println(Arrays.toString(book_s));
    }

}

class Book_ {

    /** 名称 */
    private String name;

    /**
     * 价格
     * */
    private Double price;

    public Book_(String name, Double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Book_{");
        sb.append("name='").append(name).append('\'');
        sb.append(", price=").append(price);
        sb.append('}');
        return sb.toString();
    }
}